<template>
  <div class="app-container">
    <el-row>
      <el-col class="name_s">
        <el-form :inline="true" :model="searchData">
          <el-form-item>
            <el-input v-model="searchData.name" size="medium" placeholder="输入快递名称搜索" clearable @clear="init" />
          </el-form-item>

          <el-form-item>
            <el-button type="warning" size="medium" icon="el-icon-search" @click="handerSearch">搜索
            </el-button>
            <router-link to="express/create">
              <el-button type="primary" size="medium" style="margin-left: 10px"><i class="el-icon-plus" /> 新增物流</el-button>
            </router-link>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-card>
      <el-table v-loading="listLoading" :data="expresses" element-loading-text="Loading" class="table_style" border>
        <el-table-column label="ID" prop="id" width="80" />
        <el-table-column prop="name" label="物流名称" />
        <el-table-column prop="code" label="物流编号" />
        <el-table-column label="官方链接">
          <template slot-scope="scope">
            <el-tag type="warning" style="cursor: pointer" @click="go(scope.row.url)">点击查看</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="排序">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sort" @change="changeSort(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            <span>{{ scope.row.createdAt|time_format }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import '../../filter'
import { fetchExpressList, ChangeSortExpress, deleteExpress } from '@/api/express'

export default {
  data() {
    return {
      expresses: [],
      listLoading: true,
      searchData: {
        name: ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      this.listLoading = true
      const res = await fetchExpressList(this.searchData)
      this.expresses = res.data.expresses
      this.listLoading = false
    },
    handerSearch() {
      this.init()
    },
    // 点击查看从新窗口中打开官网
    go(src) {
      window.open(src, '_blank')
    },
    // 修改排序
    async changeSort(row) {
      const res = await ChangeSortExpress(row.id, row)
      this.init()
      this.$message.success(res.message)
    },
    // 点击跳转到编辑页面
    handleEdit(row) {
      this.$router.push({
        name: 'ExpressEdit',
        params: { id: row.id }
      })
    },
    // 删除物流
    async handleDelete(row) {
      this.$confirm('确认删除该物流吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async() => {
        const res = await deleteExpress(row.id)
        this.init()
        this.$message({
          type: 'success',
          message: res.message
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
